extends Control

# label中的text文字
var text_start_line:int = 0
var text_end_line:int
# text矩阵值
var now_text:int = 0
var len_text:int
var len_text_in_dialog:int
var now_role:String
var is_history:bool = false
var is_setup:bool = false

var text

onready var tween = $Tween
onready var label = $Label
onready var audio = $Audio
onready var timer = $Timer
onready var textedit = $history/TextEdit
onready var auto_timer = $auto/Auto_Timer
onready var setup_control = $setup/Control
onready var setup_music = $setup/Control/Music
onready var setup_fx = $setup/Control/FX
onready var setup_auto = $setup/Control/auto

func _ready():
	setup_music.value = AudioServer.get_bus_volume_db(1)
	setup_fx.value = AudioServer.get_bus_volume_db(2)
	setup_auto.value = global.auto_speed
	len_text = len(text) - 1
	pass

# ==========================打字动画====================================
func _load_dialog():
	if now_text > len_text:
		get_parent().time_load += 1
		get_parent().main()
		queue_free()
	else:
		# 判别是否属于角色, 修正角色名称
		if text[now_text][0].begins_with(">"):
			if text[now_text+1] != "hide":
				# 修改角色属性
				get_parent().show_role(text[now_text+1],text[now_text+2])
				# 设置角色名称
				fix_role_name(3)
			else:
				# 存档设置
				global.left_role_path = "hide"
				global.right_role_path = "hide"
				# 修改角色属性
				get_parent().role_left.hide()
				get_parent().role_right.hide()
				# 设置角色名称
				fix_role_name(2)
		else:
			# 计算是否超过对话框
			len_text_in_dialog += 1
			if not len_text_in_dialog % 3:
				label.text = "【" + now_role + "】\n"
				text_end_line = len(now_role) + 2
				label.visible_characters = text_end_line
		
		# 计算指针位置
		text_start_line = text_end_line
		label.text += text[now_text] + "\n"
		text_end_line += len(text[now_text])
		global.dialog_data.append(text[now_text])
		
		# 音频部分
		audio.play(0.0)
		timer.wait_time = (text_end_line - text_start_line) * global.audio_speed
		timer.start()
		
		# 动画部分
		tween.interpolate_property(
			label,
			"visible_characters",
			text_start_line,
			text_end_line,
			(text_end_line - text_start_line) * global.text_speed,
			tween.TRANS_LINEAR,tween.EASE_IN_OUT
			)
		tween.start()
		now_text += 1
		
	# 判定是否自动
	if global.is_auto:
		auto_timer.wait_time = (text_end_line - text_start_line) * global.auto_speed
		auto_timer.start()
	pass

# _load_dialog()函数部分, 主要用于设置角色名称
func fix_role_name(delta_now_text:int):
	text_start_line = 0
	now_role = text[now_text]
	now_role.erase(0,1)
	label.text = "【" + now_role + "】\n"
	text_end_line = len(now_role) + 2
	label.visible_characters = text_end_line
	now_text += delta_now_text
	len_text_in_dialog = 0
	global.dialog_data.append("【" + now_role + "】")

# dialog按钮,用于更新函数
func _on_text_button_pressed():
	_load_dialog()
	global.is_auto = false
	pass

# 历史键位按下
func _on_history_pressed():
	is_history = not is_history
	setup_control.hide()
	textedit.text = ""
	for i in global.dialog_data:
		textedit.text += i + "\n"
	textedit.visible = is_history
	if is_history:
		is_setup = false
		setup_control.hide()
	pass

# 自动键位按下
func _on_auto_pressed():
	global.is_auto = not global.is_auto
	if global.is_auto:
		_load_dialog()
	pass

func _on_Auto_Timer_timeout():
	_load_dialog()
	pass

# 设置键位按下
func _on_setup_pressed():
	is_setup = not is_setup
	textedit.text = ""
	textedit.visible = is_setup
	setup_control.visible = is_setup
	if is_setup:
		is_history = false
	else:
		setup_control.hide()
	pass

# 存档键位按下
func _on_save_pressed():
	global.save_ezgal("ezgal",get_parent().time_load,now_text,now_role)
	pass

# 设置部分UI
func _on_Music_value_changed(value):
	AudioServer.set_bus_volume_db(1,value)
	pass

func _on_FX_value_changed(value):
	AudioServer.set_bus_volume_db(2,value)
	pass
	
func _on_auto_value_changed(value):
	global.auto_speed = value
	pass

# 计时器,用于结束音频
func _on_Timer_timeout():
	audio.stop()
	pass











